<?php

//Config Datei initial bereitsstellen
$config_xml;
$version;
$theme;
$iconPath;

if (isset($_GET["version"])) {
	$version = $_GET["version"];
}
if (isset($_GET["theme"])) {
	$theme = $_GET["theme"];
}

global $config_xml, $version, $theme, $iconPath;

$theme_popup;
$theme_data;
$theme_overlay;
$theme_list_menu;
$theme_header_footer;


if (file_exists("config.xml")) {
	$config_xml = simplexml_load_file("config.xml");
} else {
	exit("Konnte Datei nicht laden. ");
}

// Pfad für Icon setzen
$path = "/config/General";

	$iconPathXML = $config_xml -> xpath($path);
	$iconPath = $iconPathXML[0]->PathIcon;
//	$iconPath = $config_xml->PathIcon;

// Standardversion und theme setzen, wenn sie nicht übergeben werden
$version = set_input_version($version);
set_input_theme($theme);

function set_input_theme($i_theme) {
	global $theme_popup, $theme_data, $theme_overlay, $theme_list_menu, $theme_header_footer, $iconPath;
	switch ($i_theme) {
		case 'a' :
			$theme_popup = "data-theme=a";
			$theme_data = "data-theme=a";
			$theme_list_menu = "data-theme=a";
			$theme_overlay = "data-overlay-theme=a";
			$theme_header_footer = "data-theme=a";
			$theme_data_content = "data-content-theme=b";
			break;
		default :
			$theme_popup = "data-theme=b";
			$theme_data = "data-theme=b";
			$theme_overlay = "data-overlay-theme=b";
			$theme_list_menu = "data-theme=b";
			$theme_header_footer = "data-theme=a";
			$theme_data_content = "data-content-theme=b";
			break;
	}
}

function set_input_version($i_version) {

	switch ($i_version) {
		case 'Page_Mobile' :
			return 'Page_Mobile';
			break;
		default :
			return 'Page_Tab';
			break;
	}
}

function print_top_of_page($title) {
	echo "
<!DOCTYPE html>

<html>

	<head>

		<meta name=viewport content='user-scalable=yes'/>

		<link rel=stylesheet href=css/jquery.mobile.css />
		<link rel=stylesheet href=css/additional_styles.css />

		<script src=jquery/jquery.js></script>
		<script src=jquery/jquery.mobile.js></script>
		<script src=jquery/function.js></script>
		<script src=jquery/jquery.knob.js></script>
		
		<title>$title</title>
	</head>
	

	<body>";

}

function print_end_of_page() {

	echo "
	</body>
</html>";
}

function print_page_start($prefix, $pageNum) {
				global $theme_data;
			
	
						echo "
		<div data-role='page' id='$prefix$pageNum' $theme_data style='margin-left: 0; margin-right: 0;'>
			";
		
	
}

/*
 * Für jeden Slider wird eine Funktion gebunden, damit später darauf reagiert werden kann
 *
 */
function print_slider_script_actors() {
	$actoren_tab = array();
	$Page = 0;
	echo "
<script language='JavaScript' type='text/javascript'>";

	global $config_xml, $version;
	$actor_id;
	$i = 0;

	$path = "/config/Actors/Actor";
	if (!$res = $config_xml -> xpath($path)) {
		echo "keine Actoren vorhanden!";
	} else {

		foreach ($res as $actor) {
			//	var_dump($actor);

			$Page = $actor -> $version;
			$ActorID = $actor['ID'];
			$path = $version;
			$Page_XML = $actor -> xpath($path);
			$SortID = $Page_XML[0]['sortID'];
			//	$SortID = $Page_XML['sortID'];
			$actoren_tab[] = array('Page' => (STRING)$Page, 'sortID' => (STRING)$SortID, 'ActorID' => (STRING)$ActorID);

		}

	}
	// Sortieren der Aktoren

	# get a list of sort columns and their data to pass to array_multisort
	$sort = array();
	foreach ($actoren_tab as $key => $row) {
		$PageColumn[$key] = $row['Page'];
		$SortColumn[$key] = $row['sortID'];
	}
	# sort by event_type desc and then title asc
	array_multisort($PageColumn, SORT_ASC, $SortColumn, SORT_ASC, $actoren_tab);

	// Nur wenn Aktoren vorhanden, dann auch weitermachen
	foreach ($actoren_tab as $actoren_tab_line) {

		$path = '/config/Actors/Actor[@ID=\'' . $actoren_tab_line['ActorID'] . '\']';
		$actor_xml = $config_xml -> xpath($path);

		if (isDimmer($actor_xml[0])) {
			$actor_id = $actoren_tab_line['ActorID'];
			echo "
	$('#Actors_Slider$i').live('slidestop', function(event) {
		perform_light_change('$actor_id', 'DIM', $(this).val());  
	});";
		$i = $i + 1;
		};

	}

	echo "
</script>";
}

/*
 * Ist ein Actor ein Dimmer
 */
function isDimmer($actor) {
	if ($actor -> Dim == 'DIM') {
		return TRUE;
	} else {
		return FALSE;
	};
}

function print_menu_popup($num) {

	// Holen der globalen Variablen
	global $version, $theme_popup, $theme_data, $theme_overlay, $theme_list_menu, $theme_header_footer;

	echo "
	
			<div data-role='popup' id='popupMenu$num' $theme_popup>
				<ul data-role='listview' data-inset='true' style='min-width:210px;' $theme_list_menu>
					<li data-role='divider' $theme_header_footer>
						Men&uuml;
					<li>
						<a href='lightmanager.php' data-ajax=false>Aktoren</a>
					</li>
					<li>
						<a href='map.php' data-ajax=false>Karte</a>
					</li>
					<li>
						<a href='szenen.php' data-ajax=false>Szenen</a>
					</li>
				</ul>
			</div>";

}

function print_header_light($header) {

	$button_right = "<a href=# onClick=\"alles_aus();\" data-role=button id=ausButton data-icon=custom data-iconpos=notext></a>";
	print_header_general($header, $button_right);

}

function print_header_general($header, $button_right) {

	global $theme_header_footer;

	echo "
			<div data-role='header'>
				<h1>$header</h1>
				<a href=index.php data-role=button data-icon=home data-iconpos=notext $theme_header_footer data-ajax=false></a>
				$button_right
			</div>";
}

function print_footer_light($prefix, $pageNum, $last) {

	global $theme_header_footer;

	echo "
	
			<div data-role=footer data-position=fixed id=footer_actors $theme_header_footer>
				<div data-role=navbar>
					<ul>
						<li>";
	// auf der ersten Seite kein zurück
	if ($pageNum > 0) {
		$prev_page = $pageNum - 1;
		echo "
							<a href='#$prefix$prev_page'>zur&uuml;ck</a>";

	}

	echo "
						</li>
						<li>
							<a href='#popupMenu$pageNum' data-rel='popup' data-role='button' data-inline='true'>Men&uuml;</a>
						</li>
						<li>";
	if ($last) {
	} else {
		$next_page = $pageNum + 1;
		echo "				

							<a href='#$prefix$next_page'>weiter</a>";
	}
	echo "
						</li>
					</ul>
				</div>
			</div>
			";

}

function print_page_header($theme) {

	echo "
			<div data-role=content $theme >";
}

function print_page_end() {
	echo "
		</div>";
}

/* Ausgabe der eigentlichen Seite
 * ausgelagert in weitere PHP Dateien der besseren Übersichtlichkeit wegen
 */

include 'actors_page.php';
include 'szenen_page.php';
?>

